﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QXBook.Model;
using QXBook.Utils;
using QXBook.Service;
using Web.App_Code;

namespace Web
{
    public partial class Register : UserBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Title = "用户注册";

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private UsersInfo GetModel()
        {
            UsersInfo model = new UsersInfo();
            string username = CommonUtils.Post("iUserName");
            string password = CommonUtils.Post("iPassWord");
            string password2 = CommonUtils.Post("iPassWord2");
            string email = CommonUtils.Post("iEmal");

            if (string.IsNullOrEmpty(password))
            {
                base.ResponseErch("密码不能为空");
                return null;
            }
            if (string.IsNullOrEmpty(password2))
            {
                base.ResponseErch("请确认密码");
                return null;
            }
            if (password != password2)
            {
                base.ResponseErch("请确认密码");
                return null;
            }

            model.UserName = username;
            model.PassWord = CommonUtils.MD5(password2);
            model.Email = email;
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                UsersInfo user = this.GetModel();

                rtmessage = ServiceFactory.CreateInstance<UsersService>().Register(user);
                resultMsg = "恭喜注册成功!";
            }
            catch (Exception ex)
            {
                base.AjaxErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}